package com._520it._04_many2many.test;


import com._520it._04_many2many.domian.Student;
import com._520it._04_many2many.domian.Teacher;
import com._520it._04_many2many.mapper.StudentMapper;
import com._520it._04_many2many.mapper.TeacherMapper;
import com._520it.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;


public class App {

    @Test
    public void testSave() {
        SqlSession session = MybatisUtil.getSession();
        TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);

        //单向的: 从teacher查询student
        //创建对象,设置属性
        Teacher teacher1 = new Teacher();
        teacher1.setName("高老师");

        Teacher teacher2 = new Teacher();
        teacher2.setName("李老师");

        Student student1 = new Student();
        student1.setName("小敏");
        Student student2 = new Student();
        student2.setName("小丽");


        //设置关系
        teacher1.getStulist().add(student1);
        teacher1.getStulist().add(student2);
        teacher2.getStulist().add(student1);
        teacher2.getStulist().add(student2);

        //保存对象
        teacherMapper.save(teacher1);
        teacherMapper.save(teacher2);
        studentMapper.save(student1);
        studentMapper.save(student2);

        //保存外键关系
        for (Student student : teacher1.getStulist()) {
            teacherMapper.saveRelation(teacher1.getId(), student.getId());
        }
        for (Student student : teacher2.getStulist()) {
            teacherMapper.saveRelation(teacher2.getId(), student.getId());
        }
        session.commit();
        session.close();
        //没有session.commit();和session.close(); 无法将结果保存到数据库


    }

    //额外SQL方式查询
    @Test
    public void testGet() {
        SqlSession session = MybatisUtil.getSession();
        TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);
        Teacher teacher = teacherMapper.get(15L);
        System.out.println(teacher);

        session.commit();
        session.close();
    }

    //显式连接方式
    @Test
    public void testGet1() {
        SqlSession session = MybatisUtil.getSession();
        TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);
        Teacher teacher = teacherMapper.get1(2L);
        System.out.println(teacher);

        session.commit();
        session.close();
    }

    //显式连接方式:查询所有的
    @Test
    public void testList() {
        SqlSession session = MybatisUtil.getSession();
        TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);
        List<Teacher> list = teacherMapper.list();
        for (Teacher teacher : list) {
            System.out.println(teacher);
        }
        session.commit();
        session.close();
    }


    //若含有外键的删除, 必须先删除外键中的关系, 再删除外检所关联的表中的数据
    @Test
    public void testDelete() {
        SqlSession session = MybatisUtil.getSession();
        TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);

        teacherMapper.deleteRelation(1L);
        teacherMapper.delete(1L);

        //含有外键的删除,直接删除挖建所关联的表的数据是报错:
         //MySQLIntegrityConstraintViolationException:
        // Cannot delete or update a parent row: a foreign key constraint fails
        List<Teacher> list = teacherMapper.list();
        for (Teacher teacher : list) {
            System.out.println(teacher);
        }
        session.commit();
        session.close();
    }


}
